package uit.cinemainfo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class TheaterActivity extends Activity {
	TheaterListViewAdapter adapter;
	ListView listViewHot;
	DBAdapter db;
	Theater[] theater;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_theater);
		
		db = new DBAdapter(this);
		try{
		db.open();
        Cursor p = db.getAllTheater();
        theater = new Theater[p.getCount()];
        int i=0;
        if(p.moveToFirst()){
        	do{
        		theater[i] = new Theater(p.getString(0), p.getInt(1), p.getString(2),p.getString(3),p.getString(4),p.getString(5),p.getString(6));
        		i++;
        	}while (p.moveToNext());
        }
        db.close();
		}catch(Exception ex){
			Log.w("Here", ex.getMessage());
		}
		
		adapter = new TheaterListViewAdapter(getApplicationContext(), R.layout.theater_listview_item, theater);
        listViewHot = (ListView)findViewById(R.id.listTheater);
        listViewHot.setAdapter(adapter);
        listViewHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView av, View v, int position, long id){
        		StartMap(position);
        	}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_theater, menu);
		return true;
	}
	
	public boolean onPrepareOptionsMenu(Menu menu){
		MenuItem item = menu.findItem(R.id.menu_theater);
		item.setEnabled(false);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_showing:
	        	StartShowing();
	            return true;
	        case R.id.menu_coming:
	        	StartComing();
	            return true;
	        case R.id.menu_news:
	        	StartNews();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public void StartComing(){
		Intent intent = new Intent(TheaterActivity.this, ComingActivity.class);
		startActivity(intent);
	}
	
	public void StartShowing(){
		Intent intent = new Intent(TheaterActivity.this, ShowingActivity.class);
		startActivity(intent);
	}
	public void StartNews(){
		Intent intent = new Intent(TheaterActivity.this, ListNewsActivity.class);
		startActivity(intent);
	}
	public void StartMap(int position){
		Intent intent = new Intent(TheaterActivity.this, MapActivity.class);
		intent.putExtra("namecine", adapter.getItem(position).namecine);
		intent.putExtra("addcine", adapter.getItem(position).addcine);
		intent.putExtra("telcine", adapter.getItem(position).telcine);
		intent.putExtra("lat", adapter.getItem(position).lat);
		intent.putExtra("lng", adapter.getItem(position).lng);
		startActivity(intent);
	}
}
